package com.gitee.cashzhang27.test.cloud.oauth.auth.server.extended.handler;

import cn.hutool.core.collection.CollUtil;
import org.springframework.context.ApplicationListener;
import org.springframework.lang.NonNull;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.security.core.Authentication;

/**
 * 认证成功事件处理器
 *
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/01/31 21:35
 */
public abstract class AbstractAuthenticationSuccessEventHandler implements
    ApplicationListener<AuthenticationSuccessEvent> {

  /**
   * Handle an application event.
   *
   * @param event the event to respond to
   */
  @Override
  public void onApplicationEvent(@NonNull AuthenticationSuccessEvent event) {
    Authentication authentication = (Authentication) event.getSource();
    if (CollUtil.isNotEmpty(authentication.getAuthorities())) {
      handle(authentication);
    }
  }

  /**
   * 处理登录成功方法
   * <p>
   * 获取到登录的authentication 对象
   *
   * @param authentication 登录对象
   */
  public abstract void handle(Authentication authentication);
}
